import sys
import os
from PySide6.QtWidgets import *
from PySide6.QtGui import *
from PySide6.QtCore import *

class Demo(QWidget):
    pageNum=0
    def __init__(self, parent=None):
        super().__init__(parent)
        self.parent = parent
        layout = QVBoxLayout()        

        toolBar=QToolBar()
        toolBar.addWidget(QLabel('ViewMode:'))
        cb=QComboBox()
        cb.addItems(QMdiArea.ViewMode.__members__.keys())
        cb.currentTextChanged.connect(self.OnViewModeChanged)
        toolBar.addWidget(cb)

        toolBar.addWidget(QLabel('WindowOrder:'))
        cb=QComboBox()
        cb.addItems(QMdiArea.WindowOrder.__members__.keys())
        cb.currentTextChanged.connect(self.OnWindowOrderChanged)
        toolBar.addWidget(cb)

        btn=QPushButton('New')
        btn.clicked.connect(self.OnNewSub)
        toolBar.addWidget(btn)

        btn=QPushButton('Remove')
        btn.clicked.connect(self.OnRemove)
        toolBar.addWidget(btn)

        btn=QPushButton('Pre')
        btn.clicked.connect(self.OnPre)
        toolBar.addWidget(btn)

        btn=QPushButton('Next')
        btn.clicked.connect(self.OnNext)
        toolBar.addWidget(btn)

        btn=QPushButton('Close')
        btn.clicked.connect(self.OnClose)
        toolBar.addWidget(btn)

        btn=QPushButton('CloseAll')
        btn.clicked.connect(self.OnCloseAll)
        toolBar.addWidget(btn)

        layout.addWidget(toolBar)

        self.mdiArea=QMdiArea()
        self.mdiArea.addSubWindow        
        layout.addWidget(self.mdiArea)

        self.setLayout(layout)
    def OnViewModeChanged(self,text):
        viewMode=QMdiArea.ViewMode[text]
        self.mdiArea.setViewMode(viewMode)
        self.parent.OnShowMessage(f'Set ViewMode: {viewMode}')
    def OnWindowOrderChanged(self,text):
        windowOrder=QMdiArea.WindowOrder[text]
        self.mdiArea.setActivationOrder(windowOrder)
        self.parent.OnShowMessage(f'Set ActivationOrder: {windowOrder}')
    def OnNewSub(self):
        windowTitle=f"SubWindow{self.pageNum}"
        self.pageNum+=1        
        window=QMdiSubWindow()
        window.setWindowTitle(windowTitle)
        window.setWidget(QLabel(windowTitle))
        self.mdiArea.addSubWindow(window)
        window.show()
        self.parent.OnShowMessage(f'Add {windowTitle}')
    def OnRemove(self):
        window=self.mdiArea.currentSubWindow()
        if window is not None:
            self.parent.OnShowMessage(f'Remove {window.windowTitle()}')
            self.mdiArea.removeSubWindow(window)
    def OnPre(self):
        self.mdiArea.activatePreviousSubWindow()
        window=self.mdiArea.currentSubWindow()
        self.parent.OnShowMessage(f'Pre {window.windowTitle()}')
    def OnNext(self):
        self.mdiArea.activateNextSubWindow()
        window=self.mdiArea.currentSubWindow()
        self.parent.OnShowMessage(f'Next {window.windowTitle()}')
    def OnClose(self):
        window=self.mdiArea.currentSubWindow()
        if window is not None:
            self.parent.OnShowMessage(f'Close {window.windowTitle()}')
            self.mdiArea.closeActiveSubWindow()
    def OnCloseAll(self):
        self.mdiArea.closeAllSubWindows()
        self.parent.OnShowMessage('Close AllSubWindows')
def runDemo(parent):
    wigdet =  Demo(parent)
    return wigdet